package com.wangzh.data.structure;

//动态数组

import java.util.Arrays;
import java.util.Iterator;
import java.util.function.Consumer;
import java.util.stream.IntStream;

/**
 *  index       0   1   2   3
 *           |    |   |   |   |
 * size=0       1                  size=1
 * size=1          3               size=2
 *                     4
 *
 *
 *
 */
public class DynamicArray implements Iterable<Integer>{

    private int size = 0;
    private int capacity = 16;

    public static void main(String[] args) {


        DynamicArray dynamicArray = new DynamicArray();
        dynamicArray.addLast(1);
        dynamicArray.addLast(2);
        dynamicArray.addLast(0);

        dynamicArray.remove(1);
        dynamicArray.stream().forEach(value -> {
            System.out.println(value);
        });

//        Iterator<Integer> iterator = dynamicArray.iterator();
//        while (iterator.hasNext()) {
//            Integer next = iterator.next();
//            System.out.println(next);
//        }
//        for (Integer i : dynamicArray) {
//            System.out.println(i);
//        }
//        dynamicArray.forEach(e ->{
//            System.out.println(e);
//        });
//        for (int i = 0; i < 3; i++) {
//            System.out.println(dynamicArray.get(i));
//        }
//        System.out.println(dynamicArray.get(3));
    }

    private int[] array = new int[capacity];


    public void addLast(int element){

        add(size,element);
//        array[size]=element;
//        size++;
    }
    // |  2  |   4   |   5   |   6   |
    //index 3 插入数据7
    // |  2  |   4   |   5   |   7   |   6   |
    //index 3 之后的数据后移
    // |     |       |       |       |
    // |     |       |       |       |
    public void add(int index,int element){
        checkAndGrow();

        if (index>=0 && index<=size) {
            System.arraycopy(array,index,array,index+1,size-index );
        }
        array[index]=element;
        size++;
    }

    private void checkAndGrow() {
        //容量检查
        if (size==capacity) {
            //进行扩容
            capacity+=capacity>>1;
            int[] newArray = new int[capacity];
            System.arraycopy(array,0,newArray,0,size);
            array=newArray;
        }
    }

    public int remove(int index){
        int removed = array[index];
        System.arraycopy(array,index+1,array,index,size-index-1);
        size--;
        return removed;
    }
    public int get(int index){
        if (index<0 || index>=size) {
            throw new RuntimeException("index out of range");
        }
        return array[index];
    }
    public void foreach(Consumer<Integer> consumer){
        for (int i = 0; i < size; i++) {
//            System.out.println(array[i]);
            //提供元素
            consumer.accept(array[i]);
        }
    }

    public IntStream stream(){
        return IntStream.of(Arrays.copyOfRange(array, 0, size));
    }
    @Override
    public Iterator<Integer> iterator() {
        return new Iterator<Integer>() {
            int i =0;
            @Override
            public boolean hasNext() {//有没有下一元素
                return i<size;
            }

            @Override
            public Integer next() {//返回当前元素，并移动到下一元素
                return array[i++];
            }
        };
    }




}
